package com.ctrip.ec.api.admin.controller.brand;
import com.ctrip.ec.api.admin.model.query.QueryModel;
import com.ctrip.ec.biz.dao.image.ImageDao;
import com.ctrip.ec.dal.PagerSetting;
import com.ctrip.ec.api.admin.common.RequestContext;
import com.ctrip.ec.biz.dao.brand.BrandDao;
import com.ctrip.ec.biz.entity.brand.BrandEntity;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
@RestController
@RequestMapping("brand")
public class BrandController{
    @Autowired
    private BrandDao brand;

    @Value("${imageDomain}")
    private  String imageDomain;

    @Autowired
    private ImageDao image;

//    @GetMapping("list")
//    public List<BrandEntity> list(@RequestParam(required = false) String name) throws SQLException {
//        List<BrandEntity> list = null;
//        if(!Strings.isNullOrEmpty(name)){
//            list = brand.list("name like ?", "%" + name + "%");
//        }else {
//            list = brand.list();
//        }
//        for (BrandEntity entity : list){
//            entity.setLogoImageUrl(imageDomain + entity.getLogoImageUrl());
//            entity.setBannerImageUrl(imageDomain + entity.getBannerImageUrl());
//            entity.setCoverImageUrl(imageDomain + entity.getCoverImageUrl());
//        }
//        return list;
//    }

    @PostMapping("list")
    public List<BrandEntity> list(@RequestBody QueryModel model) throws  SQLException{
        PagerSetting setting = model.build();
        setting.appendWhere("agent_id=" + RequestContext.getAgentId());
        List<BrandEntity> list = brand.list(setting);
//        for (BrandEntity entity : list){
//            entity.setLogoImageUrl(imageDomain + entity.getLogoImageUrl());
//            entity.setBannerImageUrl(imageDomain + entity.getBannerImageUrl());
//            entity.setCoverImageUrl(imageDomain + entity.getCoverImageUrl());
//        }
        RequestContext.setResponseHeaderCount(setting.getRecordCount());
        return list;
    }

    @GetMapping("detail/{id}")
    public  BrandEntity detail(@PathVariable Long id) throws SQLException {
        return brand.entity(id);
    }

    @PostMapping("save")
    public  Long save(@RequestBody BrandEntity entity) throws SQLException {
        entity.setAgentId(RequestContext.getAgentId());
        brand.save(entity);
        image.inUse(entity.getBannerImageId());
        image.inUse(entity.getLogoImageId());
        image.inUse(entity.getCoverImageId());
        return entity.getId();
    }

    @PostMapping("delete")
    public  void delete(@RequestBody List<Long> ids) throws SQLException {
        List<Long> imageIds = brand.getImageIds(ids);
        image.unUse(imageIds);
        brand.delete(ids);
    }
    @PostMapping("erase")
    public void erase(@RequestBody List<Long> ids) throws  SQLException{
        brand.erase(ids);
    }

    @PostMapping("unerase")
    public  void unerase(@RequestBody List<Long> ids) throws  SQLException{
        brand.unerase(ids);
    }
}
